package com.atguigu.gmall.product.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:ahang
 * @create:2022-09-28 20:17
 * @Description:
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 主体拦截器
     * 执行sql的时候mybatis-plus会对所有SQL语句进行拦截并做各种判断与附加操作,
     * 会进入到Mybatis-Plus全局拦截器.sql执行时,被拦截器拦截,发现是查询语句,
     * 就会先执行winllDoQuery方法,其次做完再执行 beforeQuery.beforeQuery()方法对分页查询进行了拦截.
     * mybatisplus的分页实现其实是借助了拦截器的拦截功能,在查询之前进行了两次拦截,最终完成封装操作
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1.创建主拦截器
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //2.引入分页功能的拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOverflow(true);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);

        return mybatisPlusInterceptor;
    }
}
